/*
* 编写一个file客户端程序，对文件存储子服务进行单元测试
* 1.封装四个接口进行rpc调用，实现对于四个业务接口的测试
*/

#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include "etcd.hpp"
#include "channel.hpp"
#include "utils.hpp"
#include "file.pb.h"
#include "base.pb.h"

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试模式, true-发布模式");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志文件的路径");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志的输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "这是服务注册中心的地址");
DEFINE_string(base_service, "/service", "这是服务监控根目录");
DEFINE_string(file_service, "/service/file_service", "这是服务监控根目录");

suye::ServiceChannel::ChannelPtr channel;
std::string single_file_id;
std::vector<std::string> multi_file_id;

TEST(put_test, single_file) 
{
    // 1.读取当前目录下的指定文件数据
    std::string body;
    ASSERT_TRUE(suye::readFile("./Makefile", body));

    // 2.实例化rpc调用客户端对象，发起rpc调用
    suye::FileService_Stub stub(channel.get());

    suye::PutSingleFileReq request;
    request.set_request_id("1111");
    request.mutable_file_data()->set_file_name("Makefile");
    request.mutable_file_data()->set_file_size(body.size());
    request.mutable_file_data()->set_file_content(body);

    brpc::Controller* cntl = new brpc::Controller();
    suye::PutSingleFileRsp* response = new suye::PutSingleFileRsp();
    stub.PutSingleFile(cntl, &request, response, nullptr);
    ASSERT_FALSE(cntl->Failed());

    // 3.检测返回值中上传是否成功
    ASSERT_TRUE(response->success());
    ASSERT_EQ(response->file_info().file_size(), body.size());
    ASSERT_EQ(response->file_info().file_name(), "Makefile");
    single_file_id = response->file_info().file_id();
    LOG_DEBUG("文件ID: {}", single_file_id);
}

TEST(get_test, single_file) 
{
    // 1.先发起rpc调用，进行文件下载
    suye::FileService_Stub stub(channel.get());
    suye::GetSingleFileReq request;
    request.set_request_id("2222");
    request.set_file_id(single_file_id);
    
    brpc::Controller* cntl = new brpc::Controller();
    suye::GetSingleFileRsp* response = new suye::GetSingleFileRsp();
    stub.GetSingleFile(cntl, &request, response, nullptr);
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(response->success());

    // 2.将文件数据，存储到文件中
    ASSERT_EQ(single_file_id, response->file_data().file_id());
    suye::writeFile("Makefile_download", response->file_data().file_content());
}

TEST(put_test, multi_file) 
{
    // 1.读取当前目录下的指定文件数据
    std::string body1;
    ASSERT_TRUE(suye::readFile("./base.pb.h", body1));
    std::string body2;
    ASSERT_TRUE(suye::readFile("./file.pb.h", body2));

    // 2.实例化rpc调用客户端对象，发起rpc调用
    suye::FileService_Stub stub(channel.get());
    suye::PutMultiFileReq request;
    request.set_request_id("3333");

    auto file_data = request.add_file_data();
    file_data->set_file_name("base.pb.h");
    file_data->set_file_size(body1.size());
    file_data->set_file_content(body1);

    file_data = request.add_file_data();
    file_data->set_file_name("file.pb.h");
    file_data->set_file_size(body2.size());
    file_data->set_file_content(body2);

    brpc::Controller* cntl = new brpc::Controller();
    suye::PutMultiFileRsp* response = new suye::PutMultiFileRsp();
    stub.PutMultiFile(cntl, &request, response, nullptr);
    ASSERT_FALSE(cntl->Failed());

    // 3.检测返回值中上传是否成功
    ASSERT_TRUE(response->success());
    for (int i = 0; i < response->file_info_size(); i++) {
        multi_file_id.push_back(response->file_info(i).file_id());
        LOG_DEBUG("文件ID: {}", multi_file_id[i]);
    }
}

TEST(get_test, multi_file) 
{
    // 1.先发起rpc调用，进行文件下载
    suye::FileService_Stub stub(channel.get());
    suye::GetMultiFileReq request;
    request.set_request_id("4444");
    request.add_file_id_list(multi_file_id[0]);
    request.add_file_id_list(multi_file_id[1]);
    
    brpc::Controller* cntl = new brpc::Controller();
    suye::GetMultiFileRsp* response = new suye::GetMultiFileRsp();
    stub.GetMultiFile(cntl, &request, response, nullptr);
    ASSERT_FALSE(cntl->Failed());
    ASSERT_TRUE(response->success());

    // 2.将文件数据，存储到文件中
    ASSERT_TRUE(response->file_data().find(multi_file_id[0]) != response->file_data().end());
    ASSERT_TRUE(response->file_data().find(multi_file_id[1]) != response->file_data().end());
    auto map = response->file_data();
    auto file_data1 = map[multi_file_id[0]];
    suye::writeFile("base_download_file1", file_data1.file_content());
    auto file_data2 = map[multi_file_id[1]];
    suye::writeFile("file_download_file2", file_data2.file_content());
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    suye::Init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    // 1.先构造RPC信道管理对象
    auto service_manager = std::make_shared<suye::ServiceManager>();
    service_manager->Declared(FLAGS_file_service);
    auto put_cb = std::bind(&suye::ServiceManager::OnServiceOnline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&suye::ServiceManager::OnServiceOffline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);

    // 2.构造服务发现对象
    suye::Discovery::ptr dclient = std::make_shared<suye::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);

    // 3.通过RPC信道管理对象, 获取提供file服务的服务实例列表
    channel = service_manager->Choose(FLAGS_file_service);
    if (!channel)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        return -1;
    }

    return RUN_ALL_TESTS();
}